#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Magpie - an open source discussion forum system.
#
# Copyright (C) 2009 The Magpie Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ajiaojr.commons.django.shortcuts import render_to_response

from ajiaojr.magpie import models
from ajiaojr.magpie.views import build_template_params
from ajiaojr.magpie.views import club_writable_required
from ajiaojr.magpie.views import forum_required

from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _

@club_writable_required
@forum_required
def add(request, forum_id):
  request.user.add_favourite(request.forum)
  return HttpResponseRedirect("/")

@club_writable_required
def delete(request, forum_id):
  user_favourite = models.UserFavourite.get_by_key_name("f:%s-%d" %
      (request.user.user.email(), int(forum_id)))
  
  if user_favourite is not None:
    user_favourite.delete()
    
  return HttpResponseRedirect("/")

@club_writable_required
def manage(request):
  favourites = request.user.get_favourites()

  p = build_template_params(_('Manage Favourites'),
                              request.get_full_path(),
                              request.user)
  p['favourites'] = favourites
  return render_to_response(request, 'user/favourites.html', p)
